package com.atguigu.tingshu.account.mapper;

import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccount> {
    /**
     * 检测用户余额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Select("select count(*) from user_account where user_id = #{userId} and available_amount >= #{amount} and is_deleted = 0 for update")
    int check(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    /**
     * 锁定金额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account set available_amount = available_amount - #{amount},lock_amount = lock_amount + #{amount} where user_id = #{userId} and is_deleted = 0")
    int lock(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    /**
     * 扣减金额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account\n" +
            "set total_amount    = total_amount - #{amount},\n" +
            "    lock_amount     = lock_amount - #{amount},\n" +
            "    total_pay_amount=total_pay_amount + #{amount}\n" +
            "where user_id = #{userId}\n" +
            "  and is_deleted = 0")
    int minus(@Param("userId") Long userId, @Param("amount") BigDecimal amount);


    /**
     * 解锁金额
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account\n" +
            "set available_amount = available_amount + #{amount},\n" +
            "    lock_amount      = lock_amount - #{amount}\n" +
            "where user_id = #{userId}\n" +
            "  and is_deleted = 0;")
    int unlock(@Param("userId") Long userId, @Param("amount") BigDecimal amount);


    /**
     * 增加账户余额
     *
     * @param userId
     * @param amount
     */
    @Update("update user_account\n" +
            "set available_amount    = available_amount + #{amount},\n" +
            "    total_amount        = total_amount + #{amount},\n" +
            "    total_income_amount = total_income_amount + #{amount}\n" +
            "where user_id =  #{userId}\n" +
            "  and is_deleted = 0")
    void updateAmountByUserId(@Param("userId") Long userId, @Param("amount") BigDecimal amount);
}
